/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Vistas;

import Modelo.Canales;
import Modelo.Ciudades;
import Modelo.Clientes;
import Modelo.Funciones;
import Modelo.Servicios;
import javax.swing.DefaultListModel;

/**
 *
 * @author Ritter
 */
public class BuscarCliente extends javax.swing.JInternalFrame {

    Object[][] ciudades;
    DefaultListModel modeloListCanalesExtras = new DefaultListModel();
    DefaultListModel modeloListServiciosContratados = new DefaultListModel();

    /**
     * Creates new form Modificar
     */
    public BuscarCliente() {
        initComponents();
        this.desactivarCampos();

        //Obtengo las ciudades
        Ciudades ciu = new Ciudades();
        //Poblar canales
        for (Object[] ciudad : ciu.getCiudades()) {
            cbCiudad.addItem(ciudad[1]);
        }
        
    }

    public BuscarCliente(String rut) {
        initComponents();
        //Obtengo las ciudades
        Ciudades ciu = new Ciudades();
        //Poblar canales
        for (Object[] ciudad : ciu.getCiudades()) {
            cbCiudad.addItem(ciudad[1]);
        }

        this.buscar(rut);

        txtRut.setEnabled(false);
        
        this.permisos();
    }
    
    public void permisos(){
        switch (Principal.ROL){
            case "supervisor":
                btnModificarCliente.setEnabled(false);
                btnEliminar.setEnabled(false);
                btnAddServicio.setEnabled(false);
                cbServicios.setEnabled(false);
                btnQuitarServicio.setEnabled(false);
                btnQuitarCanal.setEnabled(false);
                btnAddCanal.setEnabled(false);
                cbCanales.setEnabled(false);
                cbPlanes.setEnabled(false);
                break;
                
            case "ejecutivo":
                //Comprobamos que el cliente no posee una deuda
                Clientes c = new Clientes();
                Object[] datos_cliente = c.getCliente(String.valueOf(txtRut.getText()));
                if(datos_cliente[4].equals("1")){
                    btnModificarCliente.setEnabled(false);
                    btnEliminar.setEnabled(false);
                    btnAddServicio.setEnabled(false);
                    cbServicios.setEnabled(false);
                    btnQuitarServicio.setEnabled(false);
                    btnQuitarCanal.setEnabled(false);
                    btnAddCanal.setEnabled(false);
                    cbCanales.setEnabled(false);
                    cbPlanes.setEnabled(false);
                }
                break;
        }        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jmnombre = new javax.swing.JLabel();
        mdeuda = new javax.swing.JLabel();
        mciudad = new javax.swing.JLabel();
        mmail = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        mfono = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        addrut = new javax.swing.JLabel();
        txtRut = new javax.swing.JTextField();
        cbCiudad = new javax.swing.JComboBox();
        chbDeuda = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listCanalesExtras = new javax.swing.JList();
        cbCanales = new javax.swing.JComboBox();
        btnAddCanal = new javax.swing.JButton();
        btnQuitarCanal = new javax.swing.JButton();
        mrut = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listServiciosContratados = new javax.swing.JList();
        cbPlanes = new javax.swing.JComboBox();
        lbPlanes = new javax.swing.JLabel();
        cbServicios = new javax.swing.JComboBox();
        btnAddServicio = new javax.swing.JButton();
        btnQuitarServicio = new javax.swing.JButton();
        txtbrut = new javax.swing.JTextField();
        jbuscar = new javax.swing.JButton();
        btnModificarCliente = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        setTitle("Buscar Cliente");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos del cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft New Tai Lue", 1, 12))); // NOI18N

        jmnombre.setText("Nombre :");

        mdeuda.setText("Deuda :");

        mciudad.setText("Ciudad :");

        mmail.setText("E-mail :");

        mfono.setText("Teléfono :");

        txtTelefono.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelefonoActionPerformed(evt);
            }
        });

        addrut.setText("Rut cliente:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addrut)
                    .addComponent(mmail)
                    .addComponent(mfono))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 39, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtRut, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(txtTelefono))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(mciudad)
                            .addComponent(jmnombre))))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(mdeuda)
                        .addGap(18, 18, 18)
                        .addComponent(chbDeuda))
                    .addComponent(cbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addrut, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtRut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jmnombre)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(mfono)
                        .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(mciudad)
                        .addComponent(cbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chbDeuda)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(mmail)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(mdeuda)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Canales Extras", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Tai Le", 1, 12))); // NOI18N

        jScrollPane2.setViewportView(listCanalesExtras);

        btnAddCanal.setText("Agregar Canal");
        btnAddCanal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddCanalActionPerformed(evt);
            }
        });

        btnQuitarCanal.setText("Quitar canal");
        btnQuitarCanal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarCanalActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnQuitarCanal, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(32, 32, 32)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbCanales, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddCanal, javax.swing.GroupLayout.Alignment.TRAILING)))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(cbCanales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAddCanal)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnQuitarCanal)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mrut.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        mrut.setText("Rut cliente:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servicios contratados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Tai Le", 1, 12))); // NOI18N

        listServiciosContratados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listServiciosContratadosMouseClicked(evt);
            }
        });
        listServiciosContratados.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listServiciosContratadosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listServiciosContratados);

        cbPlanes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbPlanesItemStateChanged(evt);
            }
        });

        lbPlanes.setText("Plan");

        btnAddServicio.setText("Agregar Servicio");
        btnAddServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddServicioActionPerformed(evt);
            }
        });

        btnQuitarServicio.setText("Quitar Servicio");
        btnQuitarServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarServicioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnAddServicio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(cbServicios, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnQuitarServicio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(cbPlanes, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(20, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lbPlanes)
                        .addGap(74, 74, 74))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnQuitarServicio)
                                .addContainerGap())
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGap(0, 29, Short.MAX_VALUE)
                                .addComponent(cbServicios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAddServicio))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(lbPlanes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbPlanes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );

        jbuscar.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        jbuscar.setText("Buscar");
        jbuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbuscarActionPerformed(evt);
            }
        });

        btnModificarCliente.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        btnModificarCliente.setText("Modificar");
        btnModificarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarClienteActionPerformed(evt);
            }
        });

        btnEliminar.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(mrut)
                .addGap(18, 18, 18)
                .addComponent(txtbrut, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62)
                .addComponent(jbuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(239, 239, 239)
                .addComponent(btnModificarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mrut)
                    .addComponent(txtbrut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnModificarCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnEliminar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void jbuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbuscarActionPerformed
        // TODO add your handling code here:
        //Si está vacío el campo 
        if (txtbrut.getText().trim().length() == 0) {
            Funciones.mensaje("Debe ingresar un rut");
        } else {
            String rut = txtbrut.getText().trim();
            this.buscar(rut);
        }
    }//GEN-LAST:event_jbuscarActionPerformed

    private void buscar(String rut) {
        Clientes c = new Clientes();
        if (c.getCliente(rut) == null) {
            Funciones.mensaje("El cliente no existe");
        } else {
            /*cl.rut, cl.nombre, cl.telefono, cl.mail, cl.deuda, cl.id_ciudad*/
            Object[] datos = c.getCliente(rut);
            txtRut.setText(String.valueOf(datos[0]));
            txtNombre.setText(String.valueOf(datos[1]));
            txtTelefono.setText(String.valueOf(datos[2]));
            txtEmail.setText(String.valueOf(datos[3]));
            //Activo o desactivo deuda
            if (String.valueOf(datos[4]).equals("1")) {
                chbDeuda.setSelected(true);
            } else {
                chbDeuda.setSelected(false);
            }

            //poblar listCanales
            this.poblarCbCanales(rut);

            //poblar listCanalesExtras
            this.poblarListCanalesExtras(rut);

            //Selecciono la ciudad
            cbCiudad.setSelectedItem(datos[5]);

            //poblar ListServiciosContratados que tiene el clienet
            this.poblarListServiciosContratados(rut);

            //poblar servicios que no tiene
            this.poblarCbServicios(rut);

            activarCampos();
        }
    }

    private void txtTelefonoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelefonoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTelefonoActionPerformed

    private void btnAddCanalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddCanalActionPerformed
        // TODO add your handling code here:

        if (cbCanales.getSelectedIndex() > -1) {
            Canales c = new Canales();
            String rut = txtRut.getText();

            int id_canal = c.getIdCanalByNombre(String.valueOf(cbCanales.getSelectedItem()));
            //Añado el canal al cliente
            c.addCanalToCliente(rut, id_canal);
            //Agrego a la lista
            modeloListCanalesExtras.addElement(cbCanales.getSelectedItem());

            //poblar CbCanales
            this.poblarCbCanales(rut);
        } else {
            Funciones.mensaje("Debe seleccionar un canal para poder agregarlo");
        }


    }//GEN-LAST:event_btnAddCanalActionPerformed

    private void btnQuitarCanalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarCanalActionPerformed
        // TODO add your handling code here:
        if (listCanalesExtras.getSelectedIndex() > -1) {
            Canales c = new Canales();
            String rut = txtRut.getText();

            int id_canal = c.getIdCanalByNombre(String.valueOf(listCanalesExtras.getSelectedValue()));
            //Borro el canal al cliente
            c.deleteCanalExtra(rut, id_canal);
            //Agrego a la lista
            modeloListCanalesExtras.removeElementAt(listCanalesExtras.getSelectedIndex());

            //poblar CbtCanales
            this.poblarCbCanales(rut);

        } else {
            Funciones.mensaje("Debe seleccionar un canal extra que desea borrar");
        }
    }//GEN-LAST:event_btnQuitarCanalActionPerformed

    private void btnAddServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddServicioActionPerformed
        // TODO add your handling code here:
        if (cbServicios.getSelectedIndex() > -1) {
            Servicios s = new Servicios();
            String rut = txtRut.getText();

            int id_servicio = s.getIdServicioByNombre(String.valueOf(cbServicios.getSelectedItem()));
            //Añado el canal al cliente
            s.addServicioToCliente(rut, id_servicio);
            //Agrego a la lista
            modeloListServiciosContratados.addElement(cbServicios.getSelectedItem());

            //poblar CbServicios
            this.poblarCbServicios(rut);
        } else {
            Funciones.mensaje("Debe seleccionar un servicio para poder agregarlo");
        }
    }//GEN-LAST:event_btnAddServicioActionPerformed

    private void btnQuitarServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarServicioActionPerformed
        // TODO add your handling code here:
        if (listServiciosContratados.getSelectedIndex() > -1) {
            Servicios s = new Servicios();
            String rut = txtRut.getText();

            int id_servicio = s.getIdServicioByNombre(String.valueOf(listServiciosContratados.getSelectedValue()));
            //Borro el servicio al cliente
            s.deleteServicioCliente(rut, id_servicio);
            //Remuevo de la lista
            modeloListServiciosContratados.removeElementAt(listServiciosContratados.getSelectedIndex());

            //elimino los planes
            cbPlanes.removeAllItems();

            //poblar CbServicios
            this.poblarCbServicios(rut);

        } else {
            Funciones.mensaje("Debe seleccionar un servicio para poder borrarlo");
        }
    }//GEN-LAST:event_btnQuitarServicioActionPerformed

    private void listServiciosContratadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listServiciosContratadosMouseClicked

    }//GEN-LAST:event_listServiciosContratadosMouseClicked

    private void listServiciosContratadosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listServiciosContratadosValueChanged
        // TODO add your handling code here:
        if (listServiciosContratados.getSelectedIndex() > -1) {
            Servicios s = new Servicios();
            String rut = txtRut.getText();

            //obtengo los planes del servicio seleccionado
            this.poblarCbPlanes(rut, s.getIdServicioByNombre(String.valueOf(listServiciosContratados.getSelectedValue())));

        }
    }//GEN-LAST:event_listServiciosContratadosValueChanged

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        Clientes c = new Clientes();
        c.deleteCliente(txtRut.getText());

        this.vaciar();
        this.desactivarCampos();

        //Obtengo las ciudades
        Ciudades ciu = new Ciudades();
        //Poblar canales
        for (Object[] ciudad : ciu.getCiudades()) {
            cbCiudad.addItem(ciudad[1]);
        }


    }//GEN-LAST:event_btnEliminarActionPerformed

    private void cbPlanesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbPlanesItemStateChanged
        // TODO add your handling code here:
        if (cbPlanes.getSelectedIndex() > -1) {
            Servicios s = new Servicios();
            String rut = txtRut.getText();
            int id_servicio = s.getIdServicioByNombre(String.valueOf(listServiciosContratados.getSelectedValue()));
            int id_plan = s.getIdPlanByNombre(String.valueOf(cbPlanes.getSelectedItem()));

            //Guardo el plan seleccionado
            s.addPlanToCliente(rut, id_servicio, id_plan);

        }

    }//GEN-LAST:event_cbPlanesItemStateChanged

    private void btnModificarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarClienteActionPerformed
        // TODO add your handling code here:
        Clientes c = new Clientes();
        Ciudades ciu = new Ciudades();
        //Compruebo que no esté vacío ningún campo
        String info[] = {txtNombre.getText(), txtEmail.getText(), txtTelefono.getText()};
 
        for (int i = 0; i < info.length; i++) {
            if (info[i].trim().length() == 0) {
                Funciones.mensaje("Debe completar todos los campos");
                return;
            }
        }
       
        //paso las validaciones, modificamos el usuario
        c.updateCliente(txtRut.getText(), txtNombre.getText(), txtTelefono.getText(), txtEmail.getText(), chbDeuda.isSelected(), ciu.getIdCiudadByNombre(String.valueOf(cbCiudad.getSelectedItem())));
        Funciones.mensaje("Cliente modificado correctamente");
        
    }//GEN-LAST:event_btnModificarClienteActionPerformed

    public void poblarCbCanales(String rut) {
        //Obtengo los canales
        Canales canales = new Canales();
        //Borro todos
        cbCanales.removeAllItems();
        //Poblar canales
        for (Object[] canal : canales.getCanalesNoTieneCliente(rut)) {
            cbCanales.addItem(canal[0]);
        }
    }

    public void poblarCbPlanes(String rut, int id_servicio) {
        Servicios ser = new Servicios();
        //Borro todos
        cbPlanes.removeAllItems();

        //Me fijo que plan tiene el cliente en ese servicio
        String plan_cliente = ser.getNombrePlanByClienteYServicio(rut, id_servicio);

        //Poblar planes
        for (Object[] plan : ser.getPlanesByServicio(id_servicio)) {
            cbPlanes.addItem(plan[0]);
        }

        //Selecciono el plan del cliente en dicho servicio
        cbPlanes.setSelectedItem(plan_cliente);

    }

    public void poblarListCanalesExtras(String rut) {
        Canales canales = new Canales();

        //borro si hay canales en la lista
        modeloListCanalesExtras.removeAllElements();

        //Agrego los canales extras que tiene el cliente
        for (Object[] canalExtra : canales.getCanalesByCliente(rut)) {
            modeloListCanalesExtras.addElement(canalExtra[0]);
        }
        listCanalesExtras.setModel(modeloListCanalesExtras);
    }

    public void poblarListServiciosContratados(String rut) {
        //poblar lista Servicios
        Servicios ser = new Servicios();

        //Borro items
        modeloListServiciosContratados.removeAllElements();

        for (Object[] servicio : ser.getServiciosByCliente(rut)) {
            modeloListServiciosContratados.addElement(servicio[0]);
        }
        listServiciosContratados.setModel(modeloListServiciosContratados);

        //Marco la primera opcion
        listServiciosContratados.setSelectedIndex(0);
    }

    public void poblarCbServicios(String rut) {
        Servicios ser = new Servicios();
        //Boro si tiene
        cbServicios.removeAllItems();
        //Poblar servicios
        for (Object[] servicio : ser.getServicioNoTieneCliente(rut)) {
            cbServicios.addItem(servicio[0]);
        }
    }

    public void desactivarCampos() {
        txtRut.setEnabled(false);
        txtNombre.setEnabled(false);
        txtTelefono.setEnabled(false);
        cbCiudad.setEnabled(false);
        txtEmail.setEnabled(false);
        chbDeuda.setEnabled(false);
        listServiciosContratados.setEnabled(false);
        listCanalesExtras.setEnabled(false);
        cbCanales.setEnabled(false);
        btnAddCanal.setEnabled(false);
        btnQuitarCanal.setEnabled(false);
        btnModificarCliente.setEnabled(false);
        btnEliminar.setEnabled(false);
        btnQuitarServicio.setEnabled(false);
        cbPlanes.setEnabled(false);
        cbServicios.setEnabled(false);
        btnAddServicio.setEnabled(false);
    }

    public void activarCampos() {
        //txtRut.setEnabled(true);
        txtNombre.setEnabled(true);
        txtTelefono.setEnabled(true);
        cbCiudad.setEnabled(true);
        txtEmail.setEnabled(true);
        chbDeuda.setEnabled(true);
        listServiciosContratados.setEnabled(true);
        listCanalesExtras.setEnabled(true);
        cbCanales.setEnabled(true);
        btnAddCanal.setEnabled(true);
        btnQuitarCanal.setEnabled(true);
        btnModificarCliente.setEnabled(true);
        btnEliminar.setEnabled(true);
        btnQuitarServicio.setEnabled(true);
        cbPlanes.setEnabled(true);
        cbServicios.setEnabled(true);
        btnAddServicio.setEnabled(true);
        
        this.permisos();
    }

    public void vaciar() {

        cbCanales.removeAllItems();
        cbServicios.removeAllItems();
        cbPlanes.removeAllItems();
        modeloListServiciosContratados.removeAllElements();
        modeloListCanalesExtras.removeAllElements();
        txtbrut.setText("");
        txtRut.setText("");
        txtNombre.setText("");
        txtEmail.setText("");
        txtTelefono.setText("");
        chbDeuda.setSelected(false);
        btnEliminar.setEnabled(false);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel addrut;
    private javax.swing.JButton btnAddCanal;
    private javax.swing.JButton btnAddServicio;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificarCliente;
    private javax.swing.JButton btnQuitarCanal;
    private javax.swing.JButton btnQuitarServicio;
    private javax.swing.JComboBox cbCanales;
    private javax.swing.JComboBox cbCiudad;
    private javax.swing.JComboBox cbPlanes;
    private javax.swing.JComboBox cbServicios;
    private javax.swing.JCheckBox chbDeuda;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbuscar;
    private javax.swing.JLabel jmnombre;
    private javax.swing.JLabel lbPlanes;
    private javax.swing.JList listCanalesExtras;
    private javax.swing.JList listServiciosContratados;
    private javax.swing.JLabel mciudad;
    private javax.swing.JLabel mdeuda;
    private javax.swing.JLabel mfono;
    private javax.swing.JLabel mmail;
    private javax.swing.JLabel mrut;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtRut;
    private javax.swing.JTextField txtTelefono;
    private javax.swing.JTextField txtbrut;
    // End of variables declaration//GEN-END:variables
}
